package com.mine.distinctionteam.ui.home.adapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.home.HomeDatasBean;
import com.mine.distinctionteam.ui.myclass.ClassActivity;
import java.util.List;

/**
 *
 * 代替之前的ListView   使用recyclerView
 */
public class ItemRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<HomeDatasBean> list;
    private LayoutInflater inflater;
    private ItemListViewRecyclerViewAdapter itemListViewRecyclerViewAdapter;

    public ItemRecyclerViewAdapter(Context mContext, List<HomeDatasBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.item_fragment_home_listview,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position>3) {
            itemListViewRecyclerViewAdapter=new ItemListViewRecyclerViewAdapter(mContext,list.get(position).getItems());
            ((ViewHolder)holder).recyclerView_item_fragment_home_listView.setAdapter(itemListViewRecyclerViewAdapter);
            LinearLayoutManager manager=new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            ((ViewHolder)holder).recyclerView_item_fragment_home_listView.setLayoutManager(manager);
            ((ViewHolder)holder).textView_item_fragment_home_listView_chinese_bag_title.setText(list.get(position).getAlbum().getTitle());
            ((ViewHolder)holder).textView_item_fragment_home_listView_bag_subTitle.setText(list.get(position).getAlbum().getSubTitle());
            if ((list.get(position).getAlbum().getTag()).equals("闲置")) {
                ((ViewHolder)holder).textView_item_fragment_home_listView_free.setText(list.get(position).getAlbum().getTag());
                ((ViewHolder)holder).textView_item_fragment_home_listView_bag_subTitle.setVisibility(View.GONE);
            }
            if ((list.get(position).getAlbum().getTag()).equals("海淘")) {
                ((ViewHolder)holder).textView_item_fragment_home_listView_bag_subTitle.setText(list.get(position).getAlbum().getTag());
                ((ViewHolder)holder).textView_item_fragment_home_listView_free.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView_item_fragment_home_listView;
        private RecyclerView recyclerView_item_fragment_home_listView;
        //闲置
        private TextView textView_item_fragment_home_listView_free;
        //海淘
        private TextView textView_item_fragment_home_listView_sea;
        private TextView textView_item_fragment_home_listView_chinese_bag_title;
        private TextView textView_item_fragment_home_listView_bag_subTitle;


        public ViewHolder(View itemView) {
            super(itemView);
            imageView_item_fragment_home_listView= (ImageView) itemView.findViewById(R.id.imageView_item_fragment_home_listView);
            recyclerView_item_fragment_home_listView = (RecyclerView) itemView.findViewById(R.id.recyclerView_item_fragment_home_listView);
            textView_item_fragment_home_listView_free = (TextView) itemView.findViewById(R.id.textView_item_fragment_home_listView_free);
            textView_item_fragment_home_listView_sea = (TextView) itemView.findViewById(R.id.textView_item_fragment_home_listView_sea);
            textView_item_fragment_home_listView_chinese_bag_title = (TextView) itemView.findViewById(R.id.textView_item_fragment_home_listView_chinese_bag_title);
            textView_item_fragment_home_listView_bag_subTitle = (TextView) itemView.findViewById(R.id.textView_item_fragment_home_listView_bag_subTitle);
        }
    }

    public void getId(final String myClass, final String myClassTitle, ViewHolder holder) {
        holder.imageView_item_fragment_home_listView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ClassActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("myClass",myClass);
                bundle.putString("myClassTitle",myClassTitle);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }
}